package com.sphd.Restaurant.fragments;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sphd.Restaurant.R;
import com.sphd.Restaurant.adapters.MyViewPagerAdapter;
import com.sphd.Restaurant.base.BaseFragment;
import com.sphd.Restaurant.fragments.homefragments.AllRestaurantFragment;
import com.sphd.Restaurant.fragments.homefragments.OftenAllrestaurantsFragment;
import com.sphd.Restaurant.model.Model;
import com.sphd.Restaurant.presenter.HomeP;
import com.sphd.Restaurant.utils.Constants;
import com.sphd.Restaurant.views.HomeView;
import com.sphd.Restaurant.views.MarqueeTextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * Created by Hu on 2016/9/25.
 */

public class HomeFragment extends BaseFragment implements HomeView {
    @BindView(R.id.tabs)
    TabLayout tabs;
    @BindView(R.id.change_service)
    Button changeService;
    @BindView(R.id.imageView2)
    ImageView imageView2;
    @BindView(R.id.city_tv)
    TextView cityTv;
    @BindView(R.id.layout_choose_city)
    LinearLayout layoutChooseCity;
    @BindView(R.id.address_tv)
    MarqueeTextView addressTv;
    @BindView(R.id.my_viewPager)
    ViewPager myViewPager;
    List<Fragment> fragmentList;
    MyViewPagerAdapter allRestsAdapter;
    OftenAllrestaurantsFragment oftenAllrestaurants;
    AllRestaurantFragment allRestaurantFragment;

    @Override
    public int getLayout() {
        return R.layout.fragment_home;
    }

    @Override
    public void initViews(View view) {
        ButterKnife.bind(this, view);
        initview();
    }

    private void initview() {
        if (!contains(Constants.SERVICEURL)){
            putString(Constants.SERVICE,"正式服");
            putString(Constants.SERVICEURL,"http://restaurant.zhongxs.com/Restaurant/cg/");
            putString(Constants.SERVICEBASEURL, "http://restaurant.zhongxs.com/Restaurant");

        }else {
            if (contains(Constants.ISBOSS)){
                if (getBoolean(Constants.ISBOSS)){
                    changeService.setVisibility(View.VISIBLE);
                    changeService.setOnClickListener(view -> {
                        showChooseUrlDialog();
                    });
                }
            }




        }
        if (contains(Constants.CTY)){
            if (TextUtils.isEmpty(getString(Constants.CTY))) {
                if (contains(Constants.LASTCITY)){
                    if (!TextUtils.isEmpty(getString(Constants.LASTCITY))) {
                        toast("定位失败，已为您自动选择上次浏览城市。");
                        cityTv.setText(getString(Constants.LASTCITY));
                        myViewPager.setCurrentItem(2);
                    } else {
                        chooseCity();
                    }
                }else {
                    chooseCity();
                }
            }else {
                cityTv.setText(getString(Constants.CTY));
                if (contains(Constants.ADS)){
                    if (!TextUtils.isEmpty(getString(Constants.ADS))){
                        addressTv.setText(getString(Constants.ADS));
                    }
                }
            }
        }else {
            if (contains(Constants.LASTCITY)){
                if (!TextUtils.isEmpty(getString(Constants.LASTCITY))) {
                    toast("定位失败，已为您自动选择上次浏览城市。");
                    cityTv.setText(getString(Constants.LASTCITY));
                    myViewPager.setCurrentItem(2);
                } else {
                    chooseCity();
                }
            }else {
                chooseCity();
            }
        }





        fragmentList = new ArrayList<>();
        oftenAllrestaurants = new OftenAllrestaurantsFragment();
        allRestaurantFragment = new AllRestaurantFragment();
        fragmentList.add(oftenAllrestaurants);
        fragmentList.add(allRestaurantFragment);
        String[] titles = {"常用", "全部"};
        allRestsAdapter = new MyViewPagerAdapter(getChildFragmentManager(), fragmentList, titles);
        myViewPager.setAdapter(allRestsAdapter);
        tabs.setupWithViewPager(myViewPager);


    }

    private void showChooseUrlDialog() {
        new AlertDialog.Builder(getContext()).setTitle("复选框").setMultiChoiceItems(new String[]{"正式服", "测试服", "本地服"},
                null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        if (which == 0) {
                            putString(Constants.SERVICE, "正式服");
                            putString(Constants.SERVICEURL, "http://restaurant.zhongxs.com/Restaurant/cg/");
                            putString(Constants.SERVICEBASEURL, "http://restaurant.zhongxs.com/Restaurant");
                        } else if (which == 1) {
                            putString(Constants.SERVICE, "测试服");
                            putString(Constants.SERVICEURL, "http://cake.zhongxs.com/Restaurant/cg/");
                            putString(Constants.SERVICEBASEURL, "http://cake.zhongxs.com/Restaurant");
                        } else {
                            putString(Constants.SERVICE, "本地");
                            putString(Constants.SERVICEURL, "http://192.168.1.127:8080.zhongxs.com/Restaurant/cg/");
                            putString(Constants.SERVICEBASEURL, "http://192.168.1.127:8080.zhongxs.com/Restaurant");

                        }
                        dialog.dismiss();
//                int pid = android.os.Process.myPid();

                        Intent intent = getActivity().getPackageManager().getLaunchIntentForPackage(getActivity().getPackageName());
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                        android.os.Process.killProcess(android.os.Process.myPid());


                    }
                })
                .setPositiveButton("", null).
                setNegativeButton("", null).show();
    }

    private void chooseCity() {
        SweetAlertDialog sweetAlertDialog = new SweetAlertDialog(context, SweetAlertDialog.ERROR_TYPE);
        sweetAlertDialog.setTitleText("定位失败,请手动选择城市信息。");
        sweetAlertDialog.setConfirmClickListener(sweetAlertDialog1 -> {
            sweetAlertDialog1.dismiss();
            showCityList();
        });
        sweetAlertDialog.setCanceledOnTouchOutside(false);
        sweetAlertDialog.setCancelable(false);
        sweetAlertDialog.show();
    }

    private void showCityList() {
        HomeP homeP = new HomeP(this);
        homeP.addData(Model.HTTPURL + Model.CITIESLIST);
    }

    @Override
    public void sendList(List<String> list) {
        SweetAlertDialog sweetAlertDialog = new SweetAlertDialog(context, SweetAlertDialog.HWZ_ListView, list);
        sweetAlertDialog.setTitleText("省份列表");
        sweetAlertDialog.setListViewItemClickListener(new SweetAlertDialog.OnSweetOnItemClickListener() {
            @Override
            public void onItemClick(SweetAlertDialog sweetAlertDialog, int position) {
                sendPosition(sweetAlertDialog, list.get(position));
            }
        });
        sweetAlertDialog.setCanceledOnTouchOutside(false);
        sweetAlertDialog.setCancelable(false);
        sweetAlertDialog.show();

    }

    private void sendPosition(SweetAlertDialog sweetAlertDialog, String cityName) {
        sweetAlertDialog.dismiss();
        cityTv.setText(cityName);
        addressTv.setText("暂无定位信息");
        putString(Constants.LASTCITY, cityName);
        removeKey(Constants.CTY);
        removeKey(Constants.ADS);
        myViewPager.setCurrentItem(2);
        allRestaurantFragment.setChooseRefresh();
    }

    @Override
    public void showdialog() {

    }

    @Override
    public void dissmissdialog() {

    }


    @OnClick(R.id.layout_choose_city)
    public void onClick() {
        showCityList();
    }
}
